// Copyright 2007 Google Inc.
// All Rights Reserved.

package com.google.jessewilson.codesearch.gtagssearch;

import com.google.jessewilson.codesearch.api.SearchProvider;
import com.google.jessewilson.codesearch.configuration.SearchProviderConfiguration;
import com.google.jessewilson.codesearch.io.jdom.EasyElement;
import com.google.jessewilson.codesearch.io.jdom.Externalizer;
import org.jdom.Element;

/**
 * Configuration for Gtags searches
 *
 * @author Ted Mao (tmao@google.com)
 */
public class GtagsConfiguration implements SearchProviderConfiguration {

  /**
   * Path to the gtags executable, e.g. <code>/usr/local/bin/gtags.sh</code>
   */
  private final String executable;

  private GtagsConfiguration(Builder builder) {
    this.executable = builder.executable;
  }

  public String getExecutable() {
    return executable;
  }

  public SearchProvider getSearchProvider() {
    return new GtagsSearchProvider();
  }

  public static class Builder {
    private String executable;

    public Builder executable(String executable) {
      this.executable = executable;
      return this;
    }

    public GtagsConfiguration build() {
      return new GtagsConfiguration(this);
    }
  }

  public static class MyExternalizer
      implements Externalizer<GtagsConfiguration> {

    public void writeExternal(Element target, GtagsConfiguration data) {
      EasyElement easyElement = new EasyElement(target);
      easyElement.putString("executable", data.executable);
    }

    public GtagsConfiguration readExternal(Element source) {
      EasyElement easyElement = new EasyElement(source);

      return new GtagsConfiguration.Builder()
          .executable(easyElement.getString("executable", null))
          .build();
    }
  }
}
